package com.mingqijia.gassafety.job.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.gap.basic.exception.BizException;
import com.mingqijia.gassafety.db.entity.Consumer;
import com.mingqijia.gassafety.db.entity.Dtu;
import com.mingqijia.gassafety.db.mapper.ConsumerMapper;
import com.mingqijia.gassafety.db.mapper.DtuMapper;
import com.mingqijia.gassafety.job.service.OrganizationalDepartmentExportService;
import com.mingqijia.gassafety.job.service.UserPermissionService;
import com.mingqijia.gassafety.shared.dto.UserFuncCodePermissionDTO;
import com.mingqijia.gassafety.shared.utils.ApplicationUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class UserPermissionServiceImpl implements UserPermissionService {
    @Autowired
    OrganizationalDepartmentExportService organizationalDepartmentExportService;

    @Autowired
    ConsumerMapper consumerMapper;

    @Autowired
    DtuMapper dtuMapper;

    @Override
    public UserFuncCodePermissionDTO getUserDepartPermission(List<String> spIds) {
        String spId = ApplicationUtils.getHeaderSpId();
        String userId = ApplicationUtils.getUserId();
        log.info("getUserDepartPermission,spId:{},userId:{},spIds:{}",spId,userId, JSONUtil.toJsonStr(spIds));
        return getUserDepartPermission(spId,userId);
    }

    @Override
    public UserFuncCodePermissionDTO getUserDepartPermission(String spId, String userId) {
        UserFuncCodePermissionDTO userFuncCodePermission = organizationalDepartmentExportService.getArea(spId,userId);
        if (null==userFuncCodePermission || null == userFuncCodePermission.getAuthority()){
            throw new BizException("未配置该账号的数据权限,请先配置");
        }
        int authority = userFuncCodePermission.getAuthority();
        List<String> funcCodes = userFuncCodePermission.getFuncCodes();
        //todo authority 刷为0 回退处理
        if( authority==1 && (null==funcCodes || funcCodes.isEmpty()) ){
            throw new BizException("该账号没有任何区域数据权限");
        }
        return userFuncCodePermission;
    }

    @Override
    public void checkDepartPermission(Integer consumerId, String spId, String userId) {
        UserFuncCodePermissionDTO userFuncCodePermission = getUserDepartPermission(spId,userId);
        if (0!=userFuncCodePermission.getAuthority()){//拥有所有权限
            List<String> funcCodes = userFuncCodePermission.getFuncCodes();
            Consumer consumer = consumerMapper.selectById(consumerId);
            if( StringUtils.isEmpty(consumer.getCitycode()) && StringUtils.isEmpty(consumer.getDistrictcode()) &&
                    StringUtils.isEmpty(consumer.getTowncode()) ){
                log.info("功能区信息为空，不在数据权限中，不能修改");
                throw new BizException("没有修改该数据的权限");
            }else if( StringUtils.isNotEmpty(consumer.getTowncode()) && !funcCodes.contains(consumer.getTowncode()) ){
                throw new BizException("没有修改该数据的权限");
            }else if( StringUtils.isNotEmpty(consumer.getDistrictcode()) && !funcCodes.contains(consumer.getDistrictcode()) ){
                throw new BizException("没有修改该数据的权限");
            }else if( StringUtils.isNotEmpty(consumer.getCitycode()) && !funcCodes.contains(consumer.getCitycode()) ){
                throw new BizException("没有修改该数据的权限");
            }
        }
    }

    @Override
    public void checkDepartPermission(String imei, String spId, String userId) {
        Dtu dtu = dtuMapper.selectById(imei);
        if( ObjectUtil.isNotEmpty(dtu) && null!=dtu.getConsumerId() && dtu.getConsumerId()>0 ){
            checkDepartPermission(dtu.getConsumerId(), spId, userId);
        }
    }
}
